﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

using CustomsSystem.BLL;
using CustomsSystem.Model;
using System.Collections.Generic;
using Maticsoft.Common;

namespace WEB.T_bill
{
    public partial class billView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.tbTimestart.Attributes.Add("onClick", "new WdatePicker()");
            this.tbTimestop.Attributes.Add("onClick", "new WdatePicker()");
            if (!IsPostBack)
            {
                DateTime A = new DateTime();
                A = DateTime.Today;
                tbTimestop.Text = A.AddDays(1).ToShortDateString();
                tbTimestart.Text = A.AddDays(-7).ToShortDateString();
            }
            Bind();
        }
        #region 自动完成填写公司
        [System.Web.Services.WebMethod]
        [System.Web.Script.Services.ScriptMethod]
        public static string[] GetCustomersName(string prefixText, int count)
        {
            CustomerManage cusManage = new CustomerManage();
            List<string> strArray = new List<string>();
            //string[] strArray = { "中国石油公司","中顺洁柔","中山技术",
            //"捷达捷达捷达达","时代复分","幅度为爱"};
            List<Customer> cusList = cusManage.GetModelList("");
            foreach (Customer aCus in cusList)
            {
                strArray.Add(aCus.CustomerName);
            }
            return (from m in strArray
                    where m.Contains(prefixText)
                    select m).Take(count).ToArray();
        }
        #endregion

        protected void btncheck_Click(object sender, EventArgs e)
        {

            Bind();
        }

        protected void gvTALL_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        public void Bind()
        {
            int Index = ddlQuery.SelectedIndex;
            string Query = ddlQuery.SelectedValue;
            string QueryText = tbQuery.Text.Trim();
            //搜索的整个过程
            string sql = " billtypeid=1 ";//为空的时候，默认搜索全部
            if (QueryText != "")//判断下拉列表的文本框是否为空
            {
                //下拉列表的判断
                switch (Index)
                {
                    //委托单查询只能为数字，如果不是数字的话转为0，就查不到数据，不会报错
                    case 1: if (!PageValidate.IsNumber(QueryText)) QueryText = "0"; sql = sql + " and a." + Query + " = '" + QueryText + "'"; break;

                    default: sql = sql + " and " + Query + " LIKE '%" + QueryText + "%'"; break;
                }
            }
            //日期搜索的判断
            if (tbTimestart.Text != "")
            {
                //日期截止时间为空的时候，默认为现在的时间
                if (tbTimestop.Text == "")
                {
                    DateTime timeStop = DateTime.Now;
                    sql = sql + " and agencyDate between '" + tbTimestart.Text + "' and '" + timeStop + "'";
                }
                else
                {
                    sql = sql + " and agencyDate between '" + tbTimestart.Text + "' and '" + tbTimestop.Text + "'";
                }

            }
            sql += " order by a.sheetID " + ddlorder.SelectedValue;
            //重新绑定数据库
            BillViewManage bll = new BillViewManage();
            DataSet ds = new DataSet();
            ds = bll.GetStatementOfAccount(sql);
            gvTALL.DataSource = ds;
            gvTALL.DataBind();
        }
        protected void gvTALL_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTALL.PageIndex = e.NewPageIndex;
            gvTALL.DataBind();
        }

        protected void gvTALL_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            Session["sheetID"] = gvTALL.Rows[e.NewSelectedIndex].Cells[0].Text;
            Response.Redirect("detailedAccount.aspx");
        }

        protected void ddlorder_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void gvTALL_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Session["sheetID"] =gvTALL.Rows[e.NewEditIndex].Cells[0].Text;
            Session["CustomsName"] = gvTALL.Rows[e.NewEditIndex].Cells[1].Text;
            Response.Redirect("account.aspx");
        }

       

     

    }
}